package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.p2pZcxxService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/zcxx")
public class p2pZcxxController extends JsonController{
	@Autowired
	private p2pZcxxService p2pZcxxService;
	/**
	 * @作者 js  
	 * @方法名 queryZcxxList  
	 * @功能 TODO 注册信息查询 列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryZcxxList")
	public ModelAndView queryZcxxList(HttpServletRequest request,HttpServletResponse response)throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String regStartDate = RequestUtils.getStringParameter(request, "regStartDate");
		String regEndDate = RequestUtils.getStringParameter(request, "regEndDate");
		String sfzNum = RequestUtils.getStringParameter(request, "sfzNum");
		String realName = RequestUtils.getStringParameter(request, "realName");
		String user_name = RequestUtils.getStringParameter(request, "user_name");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String tjr = RequestUtils.getStringParameter(request, "tjr");
		params.put("start", start);
		params.put("limit", limit);
		params.put("regStartDate", regStartDate);
		params.put("regEndDate", regEndDate);
		params.put("sfzNum", sfzNum);
		params.put("realName", realName);
		params.put("user_name", user_name);
		params.put("sjhm", sjhm);
		params.put("tjr", tjr);
		List<?> list=p2pZcxxService.queryZcxxList(params);
		int count=p2pZcxxService.getZcxxCount(params);
		result.put("data", list);	
		result.put("total", count);
		if (null!=page) {//新版本
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request,response,result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 exportZcxxExcelMethod  
	 * @功能 TODO 注册信息下载功能
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("exportZcxxExcelMethod")
	public ModelAndView exportZcxxExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		JSONObject params = new JSONObject();
		String regStartDate = RequestUtils.getStringParameter(request, "regStartDate");
		String regEndDate = RequestUtils.getStringParameter(request, "regEndDate");
		String sfzNum = RequestUtils.getStringParameter(request, "sfzNum");
		String realName = RequestUtils.getStringParameter(request, "realName");
		String user_name = RequestUtils.getStringParameter(request, "user_name");
		String sjhm = RequestUtils.getStringParameter(request, "sjhm");
		String tjr = RequestUtils.getStringParameter(request, "tjr");
		realName  = URLDecoder.decode(realName,"UTF-8");
		tjr  = URLDecoder.decode(tjr,"UTF-8");
		params.put("regStartDate", regStartDate);
		params.put("regEndDate", regEndDate);
		params.put("sfzNum", sfzNum);
		params.put("realName", realName);
		params.put("user_name", user_name);
		params.put("sjhm", sjhm);
		params.put("tjr", tjr);
		
		List list = p2pZcxxService.queryZcxxList(params);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getZcxxExportSearchResultData(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"注册信息");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "注册信息查询.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
	

}
